#include <header.h>

/* Usage:  */
int main(int argc, char *argv[]){                                  

    char *Port="8080";//监听端口
    char *IP="192.168.88.130";//本机IP

    //创建套接字
    int socketFD=socket(AF_INET,SOCK_STREAM,0);

    //绑定套接字到端口和IP
    int PortNet=htons(atoi(Port));//将端口字符串转化为数字，在转化为网络字节序
    struct in_addr IPNet;//ipv4地址结构体，用于接收主机字节序的IP地址
    inet_aton(IP,&IPNet);//转化为主机字节序

    struct sockaddr_in socketAddr;
    socketAddr.sin_family=AF_INET;//TCP
    socketAddr.sin_addr=IPNet;//IP
    socketAddr.sin_port=PortNet;

    //accept获取链接
    int res_connect=connect(socketFD,(struct sockaddr*)&socketAddr,sizeof(socketAddr));
    ERROR_CHECK(res_connect,-1,"connect");

    fd_set selectFD;
    while(1){
        FD_ZERO(&selectFD);
        FD_SET(STDIN_FILENO,&selectFD);
        FD_SET(socketFD,&selectFD);
        select(socketFD+1,&selectFD,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&selectFD))
        {
            char buf[100]={0};
            read(STDIN_FILENO,buf,sizeof(buf));
            int res_send=send(socketFD,buf,sizeof(buf),0);
            ERROR_CHECK(res_send,-1,"send");
        }
        if(FD_ISSET(socketFD,&selectFD)){
            char buf[100]={0};
            int res_recv=recv(socketFD,buf,sizeof(buf),0);
            ERROR_CHECK(res_recv,-1,"recv");
            if(res_recv==0){
                printf("对方结束\n");
                break;
            }
            printf("get your massage:%s\n",buf);

        }}
    close(socketFD);


    return 0;

}

